﻿Imports System.Globalization
Imports Microsoft.SharePoint.Client
Imports System.Text.RegularExpressions

Public Class ManMetWebServ

    'TermStore Id de Managed Metadata Service: a6dd0e4e-cf14-428a-9a2a-0c858187df97

    'TermSet Id de Conjunto de Términos: c20a6d83-5f13-46cc-ace0-971a9f177101
    'TermSet Id de Keywords: 01969fe7-58e0-4233-85ee-b4052ae78f45
    'TermSet Id de ArticlesTags : 570b7e10-f72e-4d92-8d7c-60a7971a1e7e

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim listName As String = "Shared Documents"
        Dim managedMetadataColumnName As String = "City"


        Dim managedMetadataField As Field = Nothing

        Using context As New ClientContext(TextBox1.Text)
            Dim web As Web = context.Web
            Dim list As List = context.Web.Lists.GetByTitle(listName)
            Dim collFields As FieldCollection = list.Fields

            Dim siteColumns As IEnumerable(Of Field) = context.LoadQuery( _
                collFields.Include(Function(field) field.InternalName, _
                                    Function(field) field.Id, _
                                    Function(field) field.SchemaXML))
            context.ExecuteQuery()

            If siteColumns IsNot Nothing AndAlso siteColumns.Count > 0 Then
                For Each oField In siteColumns
                    Dim regEx As New System.Text.RegularExpressions.Regex(managedMetadataColumnName, RegexOptions.IgnoreCase)
                    If regEx.IsMatch(oField.InternalName) Then
                        managedMetadataField = oField
                        Exit For
                    End If
                Next oField
            End If


            'If (siteColumn IsNot Nothing AndAlso siteColumn.Count() = 1) Then
            '    managedMetadataField = siteColumn.FirstOrDefault()
            'End If
        End Using

        Dim sspId As String = String.Empty
        Dim termSetId As String = String.Empty
        Dim schemaRoot As XElement = XElement.Parse(managedMetadataField.SchemaXml)

        For Each var As XElement In schemaRoot.Descendants("Property")
            Dim name As XElement = var.Element("Name")
            Dim value As XElement = var.Element("Value")

            If name IsNot Nothing AndAlso value IsNot Nothing Then
                Select Case name.Value
                    Case "SspId"
                        sspId = value.Value
                    Case "TermSetId"
                        termSetId = value.Value
                End Select

                If Not String.IsNullOrEmpty(sspId) AndAlso Not String.IsNullOrEmpty(termSetId) Then
                    Exit For
                End If
            End If
        Next

        ''Web Service
        Dim tcs As New TaxonomyWebService.Taxonomywebservice
        tcs.Credentials = System.Net.CredentialCache.DefaultCredentials
        'tcs.Credentials = New System.Net.NetworkCredential(Credentials.User, Credentials.Pass, Credentials.Domain)
        Dim siteUrl As String = TextBox1.Text
        tcs.Url = siteUrl & "/_vti_bin/TaxonomyClientService.asmx"
        tcs.PreAuthenticate = True

        'Dim termStoreIds As String = "<termStoreId>a6dd0e4e-cf14-428a-9a2a-0c858187df97</termStoreId>"
        'Dim termSetIds As String = "<termSetId>57b19455-5514-455d-aa91-d82c9602e72b</termSetId>"
        'Dim lcid As Integer = 1033
        'Dim oldtimestamp As String = "<timeStamp>633992461437070000</timeStamp>"
        Dim timeStamp As String = Nothing

        Dim oldtimestamp As String = "<timeStamp>633992461437070000</timeStamp>"
        Dim clientVersion As String = "<version>3</version>"
        Dim termStoreIds As String = "<termStoreId>" & sspId & "</termStoreId>"
        'Dim termStoreIds As String = "<termStoreIds><termStoreId>a6dd0e4e-cf14-428a-9a2a-0c858187df97</termStoreId><termStoreId>a2b47e22-11f6-483e-bdec-4e0334f48f95</termStoreId></termStoreIds>"
        Dim termSetIds As String = "<termSetId>" & termSetId & "</termSetId>"

        'Many
        'Dim termSetIds As String = "<termSetIds><termSetId>c20a6d83-5f13-46cc-ace0-971a9f177101</termSetId><termSetId>570b7e10-f72e-4d92-8d7c-60a7971a1e7e</termSetId></termSetIds>"
        'Dim oldtimestamps As String = "<clientTimeStamps><timeStamp>633992461437070000</timeStamp><timeStamp>633992461437070000</timeStamp></clientTimeStamps>"
        'Dim clientVersions As String = "<clientVersions><version>3</version><version>3</version></clientVersions>"


        Dim lcidSpanish As Integer = (New CultureInfo("es-ES")).LCID
        Dim lcidEnglish As Integer = (New CultureInfo("en-US")).LCID

        Dim result As String = tcs.GetTermSets(termStoreIds, termSetIds, lcidSpanish, oldtimestamp, clientVersion, timeStamp)

        'Dim resultEnglish As String = tcs.GetTermSets(termStoreIds, termSetIds, lcidEnglish, oldtimestamp, clientVersion, timeStamp)

        rtxtTermSetInfo.Text = result

        Dim xmlProducts As XDocument = XDocument.Parse(result)

        Dim terms As List(Of SolidQTerm) = (From term In xmlProducts.Descendants("T") _
         Select New SolidQTerm(True) With { _
         .Id = term.Attribute("a9").Value, _
         .Name = term.Element("LS").Element("TL").Attribute("a32").Value, _
         .TermStoreId = term.Element("TMS").Element("TM").Attribute("a24").Value, _
         .TermStoreName = term.Element("TMS").Element("TM").Attribute("a12").Value, _
         .TermStorePath = term.Element("TMS").Element("TM").Attribute("a40").Value _
        }).ToList
        'Console.WriteLine("TimeStamp:" & timeStamp)
    End Sub

    Private Sub btnSocialDataService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSocialDataService.Click
        ''Web Service
        Dim sds As New SolcialDataService.SocialDataService
        sds.Credentials = System.Net.CredentialCache.DefaultCredentials
        Dim siteUrl As String = TextBox1.Text
        sds.Url = siteUrl & "/_vti_bin/SocialDataService.asmx"
        sds.PreAuthenticate = True


        'Dim maxitems As String = "<maximumItemsToReturn>100</maximumItemsToReturn>"
        Dim maxitems As Integer? = 5
        'Dim result = sds.GetTagTerms(maxitems)

        'Dim useracount As String = "<userAccountName></userAccountName>"
        'Dim results = sds.GetTagTermsOfUser(useracount, maxitems)
        Dim noints = ""
        MessageBox.Show("Not implemented yet.")
    End Sub
End Class

